<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
/**
 *  查看查询结果，用于调试使用
 * @param $con
 */
function pre($con)
{
    echo "<pre>";
    print_r($con);
    echo "<pre>";
    exit();
}

/**
 *  弹窗跳转
 * @param $con
 * @param string $url
 */
function jump($con,$url=''){
    echo "<script>";
    if ($url) {
        echo "alert('".$con."');window.location = '".$url."';";
    }else{
        echo "alert('".$con."');window.history.go(-1);";
    }
    echo "</script>";
    exit;
}

/**
 * 弹窗不跳转
 * @param $con
 */
function alert($con){
    echo "<script>";
    echo "alert('".$con."');";
    echo "</script>";
}

/**
 * @param $name    上传域
 * @param $url 上传路径  '../public/static/uploads'
 */
function uploadimg($name,$url){
    $file = request()->file($name);// 获取表单上传文件
    $info = $file->validate(['size'=>2097152,'ext'=>'jpg,jpeg,png,gif'])->move( $url);
    if($info){// 成功上传后 获取上传信息
        return $info->getSaveName();// 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
    }else{
        // 上传失败获取错误信息
        return $file->getError();
    }
}
/**
 * 生成缩略图
 * @param  $imgurl 图片路径
 * @param  $width  缩略图宽度
 * @param  $height 缩略图高度
 * @param  $nopic 无图片是默认图片路径
 */
function thumb($src, $width=100, $height=100, $type=3,$noimg='404-wave.png') {

    $sourcepath = config('upload.path').$src;   // /home/wwwroot/www.tigersmell.com/public/static/uploads/20160820/42a79759f284b767dfcb2a0197904287.jpg
    $imgurl = config('upload.imgurl').dirname($src)."/"; //   /static/uploads/20160820/

    if(empty($src) || !file_exists($sourcepath)){
        return '/static/'.$noimg;
    }

    //缩略图存放路径
    $thumbpath = dirname($sourcepath)."/";  //    /home/wwwroot/www.tigersmell.com/public/static/uploads/20160820/
    $imgname = basename($src);     //  42a79759f284b767dfcb2a0197904287.jpg
    $ext = strtolower(strrchr($imgname, "."));    //  .jpg
    $imgname = ($ext === FALSE) ? $imgname : substr($imgname, 0, -strlen($ext)); //42a79759f284b767dfcb2a0197904287
    $filename = $imgname."_".$width."_".$height.$ext;     //42a79759f284b767dfcb2a0197904287_100_100.jpg

    if(file_exists($thumbpath.$filename)){
        return $imgurl.$filename;    //   /static/uploads/20160820/42a79759f284b767dfcb2a0197904287_100_100.jpg
    }

    $image = \think\Image::open($sourcepath);
    switch ($type)
    {
        case 1:
            $typename = \think\Image::THUMB_SCALING;
            break;
        case 2:
            $typename = \think\Image::THUMB_FILLED;
            break;
        case 3:
            $typename= \think\Image::THUMB_CENTER;
            break;
        case 4:
            $typename= \think\Image::THUMB_NORTHWEST;
            break;
        case 5:
            $typename= \think\Image::THUMB_SOUTHEAST;
            break;
        case 6:
            $typename= \think\Image::THUMB_FIXED;
            break;
    }
    $image->thumb($width, $height, $typename)->save($thumbpath.$filename); // /home/wwwroot/www.tigersmell.com/public/static/uploads/20160820/42a79759f284b767dfcb2a0197904287_100_100.jpg

    return $imgurl.$filename;  //  /static/uploads/20160820/42a79759f284b767dfcb2a0197904287_100_100.jpg
}
/**
 * 获取客户端IP地址
 * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
 * @param boolean $adv 是否进行高级模式获取（有可能被伪装）
 * @return mixed
 */
function get_client_ip($type = 0,$adv=false) {
    $type       =  $type ? 1 : 0;
    static $ip  =   NULL;
    if ($ip !== NULL) return $ip[$type];
    if($adv){//高级模式获取(防止伪装)
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $arr    =   explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            $pos    =   array_search('unknown',$arr);
            if(false !== $pos) unset($arr[$pos]);
            $ip     =   trim($arr[0]);
        }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip     =   $_SERVER['HTTP_CLIENT_IP'];
        }elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip     =   $_SERVER['REMOTE_ADDR'];
        }
    }elseif (isset($_SERVER['REMOTE_ADDR'])) {
        $ip     =   $_SERVER['REMOTE_ADDR'];
    }
    // IP地址合法验证
    $long = sprintf("%u",ip2long($ip));
    $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
    return $ip[$type];
}